<?php
class business extends table
{
	protected $_name	= 'my_business';
	protected $fields	= array('id', 'name', 'classid','zipcode','legal_representative',  'property', 'operations_model' ,  'location' ,  'address',  'introduction',  'contact',  'tel',  'fax',  'url',  'photo');
	protected $_primary = 'id';
	
	public function editMyBusiness($arg)
	{
		$db		= $this->getAdapter();
		$row	= $this->fetchRowByUid($_COOKIE['uid']);
		if ($row)
		{
			//$this->update($arg, "id={$row['id']}");
			$db->update("my_business", array_get($arg, $this->fields), "id={$row['id']}");
			return $row['id'];
		}
		else 
		{
			$id	= parent::insert($arg);
			$db->insert('my_business_user',array('user_id'=>$_COOKIE['uid'],'business_id'=>$id));
			return $db->lastInsertId();
		}
		
	}
	
	public function fetchRowByUid($uid)
	{
		$db	= $this->getAdapter();
		return $db->fetchRow(
			$db->select()
			->from('my_business as b')
			->from('my_business_user as u')
			->where("b.id=u.user_id")
			->where("u.user_id=$uid")
		);
	}
}
